Imports System.Reflection
Imports System.Collections.Generic
Imports System.Linq
Imports System.IO
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Input.Touch
Imports Microsoft.Xna.Framework.Input


Public Class Maze
	' Level content.        
	Public graphicsDevice As GraphicsDevice
	Public content As ContentManager
	Public levels As New List(Of Level)()
	Public rooms As New List(Of RoomNew)()
	Public player As Player

	Public sprites As New List(Of Sprite)()


	Private playerRoom As RoomNew
	Private Shared blockRoom As RoomNew


	'List for retain and load maze tiles textures
	Public Shared dContentRes As Dictionary(Of String, Object) = Nothing
	'public static Dictionary<string, SoundEffect> dSoundEffect = null;
	Public positionArrive As Vector2

	'test
	Public Shared dEffect As Effect = Nothing


	Private Sub Apoplexy()
		'
		Dim ap As New LevelApoplexy()
		ap.Serialize()

		Dim txtReader As Stream
		txtReader = Microsoft.Xna.Framework.TitleContainer.OpenStream(PrinceOfPersiaGame.CONFIG_PATH_CONTENT & PrinceOfPersiaGame.CONFIG_PATH_APOPLEXY & "Level1.xml")

		Dim levelsApoplexy As New List(Of LevelApoplexy)()

		Dim ax As System.Xml.Serialization.XmlSerializer = Nothing
		ax = New System.Xml.Serialization.XmlSerializer(GetType(Level))
		levelsApoplexy.Add(DirectCast(ax.Deserialize(txtReader), LevelApoplexy))
	End Sub

	Private Sub PopNet()

		'LOAD MXL CONTENT
		Dim txtReader As Stream
		'#if ANDROID
		'txtReader = Game.Activity.Assets.Open(@PrinceOfPersiaGame.CONFIG_PATH_CONTENT + PrinceOfPersiaGame.CONFIG_PATH_LEVELS + "LEVEL_dungeon_prison.xml");
		'#endif
		txtReader = Microsoft.Xna.Framework.TitleContainer.OpenStream(PrinceOfPersiaGame.CONFIG_PATH_CONTENT & PrinceOfPersiaGame.CONFIG_PATH_LEVELS & "LEVEL_dungeon_prison.xml")

		'txtReader = (Stream) File.OpenText(PrinceOfPersiaGame.CONFIG_PATH_CONTENT + PrinceOfPersiaGame.CONFIG_PATH_LEVELS + "LEVEL_dungeon_prison.xml");

		Dim ax As System.Xml.Serialization.XmlSerializer = Nothing
		ax = New System.Xml.Serialization.XmlSerializer(GetType(Level))
		levels.Add(DirectCast(ax.Deserialize(txtReader), Level))

		'Define and build a generic blockroom for usefull
		blockRoom = New RoomNew(Me, PrinceOfPersiaGame.CONFIG_PATH_CONTENT & PrinceOfPersiaGame.CONFIG_PATH_ROOMS & "MAP_blockroom.xml", 1)


		'LOAD ALL LEVEL IN LIST
		'System.Xml.Serialization.XmlSerializer ax;
		'Stream astream = this.GetType().Assembly.GetManifestResourceStream(path_resources + "LEVEL_dungeon_prison.xml");
		'ax = new System.Xml.Serialization.XmlSerializer(typeof(Level));
		'levels.Add((Level)ax.Deserialize(astream));


		'load all room
		For z As Integer = 0 To levels.Count() - 1
			'int newX = 1;
			For y As Integer = 0 To levels(z).rows.Count() - 1
				For x As Integer = 0 To levels(z).rows(y).columns.Count() - 1
					If levels(z).rows(y).columns(x).FilePath = String.Empty Then
						levels(z).rows(y).columns(x).FilePath = "MAP_blockroom.xml"
					End If

					Dim room As New RoomNew(Me, PrinceOfPersiaGame.CONFIG_PATH_CONTENT & PrinceOfPersiaGame.CONFIG_PATH_ROOMS & levels(z).rows(y).columns(x).FilePath, levels(z).rows(y).columns(x).RoomIndex)
					'RoomNew room = new RoomNew(this, "Maze/"+ levels[z].rows[y].columns[x].FilePath);
					room.roomStart = levels(z).rows(y).columns(x).RoomStart
					room.roomName = levels(z).rows(y).columns(x).FilePath
					room.roomZ = z
					room.roomX = x
					room.roomY = y
						'newX++;
					rooms.Add(room)
				Next
			Next
		Next
	End Sub



	Public Sub New(GraphicsDevice__1 As GraphicsDevice, contentmanager As ContentManager)
		content = contentmanager
		graphicsDevice = GraphicsDevice__1

		LoadContent()

		'dEffect = content.Load<Effect>(@"Effects\SwapColor");

		'if (PrinceOfPersiaGame.CONFIG_PATH_APOPLEXY != string.Empty)
		'    Apoplexy();
		'else



		PopNet()
	End Sub

	'Load all texture in a dictiornary
	Private Sub LoadContent()
		'dSoundEffect = Program.LoadContent<SoundEffect>(content, string.Empty);
        dContentRes = Program.LoadContent(Of Object)(content, String.Empty)


	End Sub



	Public Function GetTiles(tileType As Enumeration.TileType) As List(Of Tile)
		Dim list As New List(Of Tile)()
		For Each r As RoomNew In rooms
			For Each t As Tile In r.GetTiles(tileType)
				list.Add(t)
				'list.Concat(r.GetTiles(tileType));
			Next
		Next
		Return list
	End Function

	Public Function LeftRoom(room As RoomNew) As RoomNew
		Dim x As Integer = room.roomX
		Dim y As Integer = room.roomY
		Dim z As Integer = room.roomZ

		If x <> 0 Then
			x = System.Threading.Interlocked.Decrement(x)
		Else
			Return blockRoom
		End If
		'return new RoomNew(this, PrinceOfPersiaGame.CONFIG_PATH_CONTENT + PrinceOfPersiaGame.CONFIG_PATH_ROOMS + "MAP_blockroom.xml");

		For Each r As RoomNew In rooms
			If r.roomX = x And r.roomY = y And r.roomZ = z Then
				Return r
			End If
		Next
		Return blockRoom
		'return new RoomNew(this, PrinceOfPersiaGame.CONFIG_PATH_CONTENT + PrinceOfPersiaGame.CONFIG_PATH_ROOMS + "MAP_blockroom.xml");
	End Function

	Public Function DownRoom(room As RoomNew) As RoomNew
		Dim x As Integer = room.roomX
		Dim y As Integer = room.roomY
		Dim z As Integer = room.roomZ

		If y <> levels(z).rows.Count() - 1 Then
			y = System.Threading.Interlocked.Increment(y)
		Else
			Return blockRoom
		End If
		'return new RoomNew(this, PrinceOfPersiaGame.CONFIG_PATH_CONTENT + PrinceOfPersiaGame.CONFIG_PATH_ROOMS + "MAP_blockroom.xml");

		For Each r As RoomNew In rooms
			If r.roomX = x And r.roomY = y And r.roomZ = z Then
				Return r
			End If
		Next
		Return blockRoom
		'return new RoomNew(this, PrinceOfPersiaGame.CONFIG_PATH_CONTENT + PrinceOfPersiaGame.CONFIG_PATH_ROOMS + "MAP_blockroom.xml");
	End Function

	Public Function RightRoom(room As RoomNew) As RoomNew
		Dim x As Integer = room.roomX
		Dim y As Integer = room.roomY
		Dim z As Integer = room.roomZ

		If x <> levels(z).rows(y).columns.Count() - 1 Then
			x = System.Threading.Interlocked.Increment(x)
		Else
			Return blockRoom
		End If
		'return new RoomNew(this, PrinceOfPersiaGame.CONFIG_PATH_CONTENT + PrinceOfPersiaGame.CONFIG_PATH_ROOMS + "MAP_blockroom.xml");

		For Each r As RoomNew In rooms
			If r.roomX = x And r.roomY = y And r.roomZ = z Then
				Return r
			End If
		Next
		Return blockRoom
		'return new RoomNew(this, PrinceOfPersiaGame.CONFIG_PATH_CONTENT + PrinceOfPersiaGame.CONFIG_PATH_ROOMS + "MAP_blockroom.xml");
	End Function

	Public Function UpRoom(room As RoomNew) As RoomNew
		Dim x As Integer = room.roomX
		Dim y As Integer = room.roomY
		Dim z As Integer = room.roomZ

		If y <> levels(z).rows.Count() - 1 Then
			y = System.Threading.Interlocked.Decrement(y)
		Else
			Return blockRoom
		End If
		'return new RoomNew(this, PrinceOfPersiaGame.CONFIG_PATH_CONTENT + PrinceOfPersiaGame.CONFIG_PATH_ROOMS + "MAP_blockroom.xml");

		For Each r As RoomNew In rooms
			If r.roomX = x And r.roomY = y And r.roomZ = z Then
				Return r
			End If
		Next
		Return blockRoom
		'return new RoomNew(this, PrinceOfPersiaGame.CONFIG_PATH_CONTENT + PrinceOfPersiaGame.CONFIG_PATH_ROOMS + "MAP_blockroom.xml");
	End Function

	Public Function StartRoom() As RoomNew
		For Each r As RoomNew In rooms
			If r.roomStart = True Then
				player.SpriteRoom = r
				Return r
			End If
		Next
		Return blockRoom
		'return new RoomNew(this, PrinceOfPersiaGame.CONFIG_PATH_CONTENT + PrinceOfPersiaGame.CONFIG_PATH_ROOMS + "MAP_blockroom.xml");
	End Function


	Public Function FindRoom(roomName As String) As RoomNew
		Return Nothing
	End Function

End Class
